use Test::More;
use Data::Dumper;
use Cwd;

plan 'no_plan';

# test current path
	like(cwd(), 'm#keepconfig/perl$#');
	require_ok("./keepconfig");

# test globals
	like($HOME, qr{^/home/[^/]+}, 'test $HOME not empty');

# test split_path
	is($fake_cwd, undef, '$fake_cwd is not defined');
	$fake_cwd = "/bla/machine/myserver-01/etc";
	$loc = split_path("pipo/molo");
	isa_ok($loc, 'HASH', '$loc');
	#print Dumper($loc);

# test command_line_parse
	@ARGV = qw/--init keepconfigdir/;
	$p = command_line_parse();
	is($p, 'keepconfigdir', 'command_line_parse return value');

# error handling
	is($debug_catch_error, 0, '$debug_catch_error is false');
	is($ERROR_COUNT, 0, '$ERROR_COUNT at 0');

	error("pipo");
	is($debug_log[0], undef, "error message is displayed on stderr");

	$debug_catch_error = 1;
	error("pipo");
	is($debug_log[0], 'pipo');
	error("molo");
	is($debug_log[1], 'molo');
	is(scalar(@debug_log), 2);
	is($ERROR_COUNT, 3, "error() called 3 times");

# test init_svn_repos
	$ERROR_COUNT = 0;
	$r = init_svn_repos($p);
	is($r, 1, "init_svn_repos returned true '1'");
	is($ERROR_COUNT, 0, "no error for this call");

	# create a test repos
	$tmp_wc = "tmp/test_wc$$";
	`svn mkdir $tmp_wc`;
	ok(-d $tmp_wc, "$tmp_wc folder exists");

	# change $HOME to a fake value for testing purpose
	$tmp_HOME = "/tmp/tmp_HOME$$";
	ok(mkdir($tmp_HOME), 'create $tmp_HOME');
	$HOME = $tmp_HOME;
	ok(-d $HOME && -w $HOME, '$HOME folder is writable');

	$r = init_svn_repos($tmp_wc);
	is($ERROR_COUNT, 0, "still no error");
	ok(-e "$tmp_HOME/.keepconfig", ".keepconfig created");

	# cleanup
	`svn rm --force $tmp_wc`;
	`rm -rf $tmp_HOME`;
